package com.pzhuweb.sgblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pzhuweb.sgblog.domain.dto.RoleDto;
import com.pzhuweb.sgblog.domain.dto.RoleStatusDto;
import com.pzhuweb.sgblog.domain.entity.Role;
import com.pzhuweb.sgblog.domain.enums.AppHttpCodeEnum;
import com.pzhuweb.sgblog.domain.utils.BeanCopyUtils;
import com.pzhuweb.sgblog.domain.utils.ResponseResult;
import com.pzhuweb.sgblog.domain.vo.PageVo;
import com.pzhuweb.sgblog.domain.vo.RoleVo;
import com.pzhuweb.sgblog.exception.SystemException;
import com.pzhuweb.sgblog.mapper.MenuMapper;
import com.pzhuweb.sgblog.mapper.RoleMapper;
import com.pzhuweb.sgblog.mapper.SystemRoleMapper;
import com.pzhuweb.sgblog.service.SystemRoleService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 鄧浩奇
 * @classname SystemRoleServiceImpl
 * @description
 * @since 2023/8/21 16:56
 **/
@Service
public class SystemRoleServiceImpl extends ServiceImpl<SystemRoleMapper, Role> implements SystemRoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private MenuMapper menuMapper;

    @Override
    public ResponseResult<?> listAllRole() {
        List<Role> list = list();
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult<?> getRolePage(Integer pageNum, Integer pageSize, String roleName, String status) {
        Page<Role> rolePage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(roleName), Role::getRoleName, roleName)
                .eq(StringUtils.hasText(status), Role::getStatus, status);
        page(rolePage, wrapper);
        PageVo pageVo = new PageVo(rolePage.getRecords(), rolePage.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult<?> changeStatus(RoleStatusDto statusDto) {
        LambdaUpdateWrapper<Role> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Role::getId, statusDto.getRoleId())
                .set(Role::getStatus, statusDto.getStatus());
        update(wrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<?> insertRole(RoleDto roleDto) {
        Role role = BeanCopyUtils.copyBean(roleDto, Role.class);
        save(role);
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Role::getRoleName, roleDto.getRoleName());
        roleDto.getMenuIds()
                .forEach(menuId -> roleMapper.insertById(getOne(wrapper).getId(), menuId));
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<?> getRoleInfo(Long id) {
        Role role = getById(id);
        RoleVo roleVo = BeanCopyUtils.copyBean(role, RoleVo.class);
        return ResponseResult.okResult(roleVo);
    }

    @Override
    public ResponseResult<?> updateRole(RoleDto roleDto) {
        if (!StringUtils.hasText(roleDto.getRoleName())) {
            throw new SystemException(AppHttpCodeEnum.ROLE_NAME_ISNULL);
        }
        if (!StringUtils.hasText(roleDto.getRoleKey())) {
            throw new SystemException(AppHttpCodeEnum.ROLE_KEY_ISNULL);
        }
        LambdaUpdateWrapper<Role> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Role::getId, roleDto.getId());
        Role role = BeanCopyUtils.copyBean(roleDto, Role.class);
        update(role, wrapper);
        // 更改关联的菜单列表
        List<Long> menuIds = roleDto.getMenuIds();
        menuMapper.deleteMenuIdsByRoleId(roleDto.getId());
        if (!roleDto.getMenuIds().isEmpty()) {
            menuIds.forEach(menuId -> roleMapper.insertById(roleDto.getId(), menuId));
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<?> deleteRoleById(List<Long> roleIds) {
        // 删除角色信息
        removeBatchByIds(roleIds);
        // 删除关联的菜单
        roleIds.forEach(id->menuMapper.deleteMenuIdsByRoleId(id));
        return ResponseResult.okResult();
    }
}
